namespace PMS.BLL
{
    using PMS.Common;
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganChangeLogBLL
    {
        public int Add(OrganChangeLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganChangeLog(");
            builder.Append("OrganClassID,OrganRoleID,OrganLevelID,OrganTypeID,OrganBuildID,OrganDescription,OrganBranchCode,PermitNo,Functions,BankName,OrganID,BankID,LegalPerson,PermitOrganCount,RealityOrganCount,Address,Postcode,Contact,Tel,Remark,AddDate,OrganName,AddUser,UpdateDate,UpdateUser,LogType,CheckedState,CheckDate,CheckAdminID,CheckRemark,OrganName2,PayWay,IsOffice,LevelNo,ParentID,ParentName,OrganCode");
            builder.Append(") values (");
            builder.Append("@OrganClassID,@OrganRoleID,@OrganLevelID,@OrganTypeID,@OrganBuildID,@OrganDescription,@OrganBranchCode,@PermitNo,@Functions,@BankName,@OrganID,@BankID,@LegalPerson,@PermitOrganCount,@RealityOrganCount,@Address,@Postcode,@Contact,@Tel,@Remark,@AddDate,@OrganName,@AddUser,@UpdateDate,@UpdateUser,@LogType,@CheckedState,@CheckDate,@CheckAdminID,@CheckRemark,@OrganName2,@PayWay,@IsOffice,@LevelNo,@ParentID,@ParentName,@OrganCode");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@OrganClassID", model.OrganClassID), new SqlParameter("@OrganRoleID", model.OrganRoleID), new SqlParameter("@OrganLevelID", model.OrganLevelID), new SqlParameter("@OrganTypeID", model.OrganTypeID), new SqlParameter("@OrganBuildID", model.OrganBuildID), new SqlParameter("@OrganDescription", model.OrganDescription), new SqlParameter("@OrganBranchCode", model.OrganBranchCode), new SqlParameter("@PermitNo", model.PermitNo), new SqlParameter("@Functions", model.Functions), new SqlParameter("@BankName", model.BankName), new SqlParameter("@OrganID", model.OrganID), new SqlParameter("@BankID", model.BankID), new SqlParameter("@LegalPerson", model.LegalPerson), new SqlParameter("@PermitOrganCount", model.PermitOrganCount), new SqlParameter("@RealityOrganCount", model.RealityOrganCount), new SqlParameter("@Address", model.Address), 
                new SqlParameter("@Postcode", model.Postcode), new SqlParameter("@Contact", model.Contact), new SqlParameter("@Tel", model.Tel), new SqlParameter("@Remark", model.Remark), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@UpdateDate", model.UpdateDate), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@LogType", model.LogType), new SqlParameter("@CheckedState", model.CheckedState), new SqlParameter("@CheckDate", model.CheckDate), new SqlParameter("@CheckAdminID", model.CheckAdminID), new SqlParameter("@CheckRemark", model.CheckRemark), new SqlParameter("@OrganName2", model.OrganName2), new SqlParameter("@PayWay", model.PayWay), 
                new SqlParameter("@IsOffice", model.IsOffice), new SqlParameter("@LevelNo", model.LevelNo), new SqlParameter("@ParentID", model.ParentID), new SqlParameter("@ParentName", model.ParentName), new SqlParameter("@OrganCode", model.OrganCode)
             };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Check(int logID, string checkState, string CheckRemark)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganChangeLog set ");
            builder.Append(" CheckedState = @CheckedState , ");
            builder.Append(" CheckDate = @CheckDate , ");
            builder.Append(" CheckAdminID = @CheckAdminID , ");
            builder.Append(" CheckRemark = @CheckRemark  ");
            builder.Append(" where LogID=@LogID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", logID), new SqlParameter("@CheckedState", checkState), new SqlParameter("@CheckDate", DateTime.Now), new SqlParameter("@CheckAdminID", Keys.Admin.AdminID), new SqlParameter("@CheckRemark", CheckRemark) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Delete(int LogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganChangeLog ");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LogID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string LogIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganChangeLog ");
            builder.Append(" where ID in (" + LogIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<OrganChangeLog> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM view_OrganChangeLog ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            OrganChangeLog item = null;
            List<OrganChangeLog> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<OrganChangeLog>();
                foreach (DataRow row in table.Rows)
                {
                    item = new OrganChangeLog();
                    if (row["LogID"].ToString() != "")
                    {
                        item.LogID = int.Parse(row["LogID"].ToString());
                    }
                    if (row["OrganClassID"].ToString() != "")
                    {
                        item.OrganClassID = new int?(int.Parse(row["OrganClassID"].ToString()));
                    }
                    if (row["OrganRoleID"].ToString() != "")
                    {
                        item.OrganRoleID = new int?(int.Parse(row["OrganRoleID"].ToString()));
                    }
                    if (row["OrganLevelID"].ToString() != "")
                    {
                        item.OrganLevelID = new int?(int.Parse(row["OrganLevelID"].ToString()));
                    }
                    if (row["OrganTypeID"].ToString() != "")
                    {
                        item.OrganTypeID = new int?(int.Parse(row["OrganTypeID"].ToString()));
                    }
                    if (row["OrganBuildID"].ToString() != "")
                    {
                        item.OrganBuildID = new int?(int.Parse(row["OrganBuildID"].ToString()));
                    }
                    item.OrganDescription = row["OrganDescription"].ToString();
                    item.OrganBranchCode = row["OrganBranchCode"].ToString();
                    item.PermitNo = row["PermitNo"].ToString();
                    item.Functions = row["Functions"].ToString();
                    item.BankName = row["BankName"].ToString();
                    if (row["OrganID"].ToString() != "")
                    {
                        item.OrganID = int.Parse(row["OrganID"].ToString());
                    }
                    item.BankID = row["BankID"].ToString();
                    item.LegalPerson = row["LegalPerson"].ToString();
                    if (row["PermitOrganCount"].ToString() != "")
                    {
                        item.PermitOrganCount = new int?(int.Parse(row["PermitOrganCount"].ToString()));
                    }
                    if (row["RealityOrganCount"].ToString() != "")
                    {
                        item.RealityOrganCount = new int?(int.Parse(row["RealityOrganCount"].ToString()));
                    }
                    item.Address = row["Address"].ToString();
                    item.Postcode = row["Postcode"].ToString();
                    item.Contact = row["Contact"].ToString();
                    item.Tel = row["Tel"].ToString();
                    item.Remark = row["Remark"].ToString();
                    if (row["AddDate"].ToString() != "")
                    {
                        item.AddDate = new DateTime?(DateTime.Parse(row["AddDate"].ToString()));
                    }
                    item.OrganName = row["OrganName"].ToString();
                    item.AddUser = row["AddUser"].ToString();
                    if (row["UpdateDate"].ToString() != "")
                    {
                        item.UpdateDate = new DateTime?(DateTime.Parse(row["UpdateDate"].ToString()));
                    }
                    item.UpdateUser = row["UpdateUser"].ToString();
                    item.LogType = row["LogType"].ToString();
                    item.CheckedState = row["CheckedState"].ToString();
                    if (row["CheckDate"].ToString() != "")
                    {
                        item.CheckDate = new DateTime?(DateTime.Parse(row["CheckDate"].ToString()));
                    }
                    if (row["CheckAdminID"].ToString() != "")
                    {
                        item.CheckAdminID = int.Parse(row["CheckAdminID"].ToString());
                    }
                    item.CheckRemark = row["CheckRemark"].ToString();
                    item.OrganName2 = row["OrganName2"].ToString();
                    item.PayWay = row["PayWay"].ToString();
                    if (row["IsOffice"].ToString() != "")
                    {
                        item.IsOffice = int.Parse(row["IsOffice"].ToString());
                    }
                    if (row["LevelNo"].ToString() != "")
                    {
                        item.LevelNo = new int?(int.Parse(row["LevelNo"].ToString()));
                    }
                    if (row["ParentID"].ToString() != "")
                    {
                        item.ParentID = new int?(int.Parse(row["ParentID"].ToString()));
                    }
                    item.ParentName = row["ParentName"].ToString();
                    item.OrganCode = row["OrganCode"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganChangeLog GetModel(int LogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append("  from OrganChangeLog ");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LogID;
            OrganChangeLog log = new OrganChangeLog();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["LogID"].ToString() != "")
                {
                    log.LogID = int.Parse(row["LogID"].ToString());
                }
                if (row["OrganClassID"].ToString() != "")
                {
                    log.OrganClassID = new int?(int.Parse(row["OrganClassID"].ToString()));
                }
                if (row["OrganRoleID"].ToString() != "")
                {
                    log.OrganRoleID = new int?(int.Parse(row["OrganRoleID"].ToString()));
                }
                if (row["OrganLevelID"].ToString() != "")
                {
                    log.OrganLevelID = new int?(int.Parse(row["OrganLevelID"].ToString()));
                }
                if (row["OrganTypeID"].ToString() != "")
                {
                    log.OrganTypeID = new int?(int.Parse(row["OrganTypeID"].ToString()));
                }
                if (row["OrganBuildID"].ToString() != "")
                {
                    log.OrganBuildID = new int?(int.Parse(row["OrganBuildID"].ToString()));
                }
                log.OrganDescription = row["OrganDescription"].ToString();
                log.OrganBranchCode = row["OrganBranchCode"].ToString();
                log.PermitNo = row["PermitNo"].ToString();
                log.Functions = row["Functions"].ToString();
                log.BankName = row["BankName"].ToString();
                if (row["OrganID"].ToString() != "")
                {
                    log.OrganID = int.Parse(row["OrganID"].ToString());
                }
                log.BankID = row["BankID"].ToString();
                log.LegalPerson = row["LegalPerson"].ToString();
                if (row["PermitOrganCount"].ToString() != "")
                {
                    log.PermitOrganCount = new int?(int.Parse(row["PermitOrganCount"].ToString()));
                }
                if (row["RealityOrganCount"].ToString() != "")
                {
                    log.RealityOrganCount = new int?(int.Parse(row["RealityOrganCount"].ToString()));
                }
                log.Address = row["Address"].ToString();
                log.Postcode = row["Postcode"].ToString();
                log.Contact = row["Contact"].ToString();
                log.Tel = row["Tel"].ToString();
                log.Remark = row["Remark"].ToString();
                if (row["AddDate"].ToString() != "")
                {
                    log.AddDate = new DateTime?(DateTime.Parse(row["AddDate"].ToString()));
                }
                log.OrganName = row["OrganName"].ToString();
                log.AddUser = row["AddUser"].ToString();
                if (row["UpdateDate"].ToString() != "")
                {
                    log.UpdateDate = new DateTime?(DateTime.Parse(row["UpdateDate"].ToString()));
                }
                log.UpdateUser = row["UpdateUser"].ToString();
                log.LogType = row["LogType"].ToString();
                log.CheckedState = row["CheckedState"].ToString();
                if (row["CheckDate"].ToString() != "")
                {
                    log.CheckDate = new DateTime?(DateTime.Parse(row["CheckDate"].ToString()));
                }
                if (row["CheckAdminID"].ToString() != "")
                {
                    log.CheckAdminID = int.Parse(row["CheckAdminID"].ToString());
                }
                log.CheckRemark = row["CheckRemark"].ToString();
                log.OrganName2 = row["OrganName2"].ToString();
                log.PayWay = row["PayWay"].ToString();
                if (row["IsOffice"].ToString() != "")
                {
                    log.IsOffice = int.Parse(row["IsOffice"].ToString());
                }
                if (row["LevelNo"].ToString() != "")
                {
                    log.LevelNo = new int?(int.Parse(row["LevelNo"].ToString()));
                }
                if (row["ParentID"].ToString() != "")
                {
                    log.ParentID = new int?(int.Parse(row["ParentID"].ToString()));
                }
                log.ParentName = row["ParentName"].ToString();
                log.OrganCode = row["OrganCode"].ToString();
                return log;
            }
            return null;
        }

        public List<view_OrganChangeLog> GetViewList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM view_OrganChangeLog ");
            builder.Append(" WHERE CheckedState= '未审核'");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            view_OrganChangeLog item = null;
            List<view_OrganChangeLog> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<view_OrganChangeLog>();
                foreach (DataRow row in table.Rows)
                {
                    item = new view_OrganChangeLog {
                        IOrganName = row["IOrganName"].ToString(),
                        OrganCode = row["OrganCode"].ToString()
                    };
                    if (row["OrganClassID"].ToString() != "")
                    {
                        item.OrganClassID = new int?(int.Parse(row["OrganClassID"].ToString()));
                    }
                    if (row["OrganRoleID"].ToString() != "")
                    {
                        item.OrganRoleID = new int?(int.Parse(row["OrganRoleID"].ToString()));
                    }
                    if (row["OrganLevelID"].ToString() != "")
                    {
                        item.OrganLevelID = new int?(int.Parse(row["OrganLevelID"].ToString()));
                    }
                    if (row["OrganTypeID"].ToString() != "")
                    {
                        item.OrganTypeID = new int?(int.Parse(row["OrganTypeID"].ToString()));
                    }
                    if (row["OrganBuildID"].ToString() != "")
                    {
                        item.OrganBuildID = new int?(int.Parse(row["OrganBuildID"].ToString()));
                    }
                    item.OrganDescription = row["OrganDescription"].ToString();
                    item.OrganBranchCode = row["OrganBranchCode"].ToString();
                    item.PermitNo = row["PermitNo"].ToString();
                    item.Functions = row["Functions"].ToString();
                    if (row["LogID"].ToString() != "")
                    {
                        item.LogID = int.Parse(row["LogID"].ToString());
                    }
                    if (row["PermitDate"].ToString() != "")
                    {
                        item.PermitDate = new DateTime?(DateTime.Parse(row["PermitDate"].ToString()));
                    }
                    item.BankName = row["BankName"].ToString();
                    item.BankID = row["BankID"].ToString();
                    item.LegalPerson = row["LegalPerson"].ToString();
                    if (row["PermitOrganCount"].ToString() != "")
                    {
                        item.PermitOrganCount = new int?(int.Parse(row["PermitOrganCount"].ToString()));
                    }
                    if (row["RealityOrganCount"].ToString() != "")
                    {
                        item.RealityOrganCount = new int?(int.Parse(row["RealityOrganCount"].ToString()));
                    }
                    item.Address = row["Address"].ToString();
                    item.Postcode = row["Postcode"].ToString();
                    item.Contact = row["Contact"].ToString();
                    item.Tel = row["Tel"].ToString();
                    if (row["OrganID"].ToString() != "")
                    {
                        item.OrganID = int.Parse(row["OrganID"].ToString());
                    }
                    item.Remark = row["Remark"].ToString();
                    if (row["AddDate"].ToString() != "")
                    {
                        item.AddDate = new DateTime?(DateTime.Parse(row["AddDate"].ToString()));
                    }
                    item.AddUser = row["AddUser"].ToString();
                    if (row["UpdateDate"].ToString() != "")
                    {
                        item.UpdateDate = new DateTime?(DateTime.Parse(row["UpdateDate"].ToString()));
                    }
                    item.UpdateUser = row["UpdateUser"].ToString();
                    item.LogType = row["LogType"].ToString();
                    item.CheckedState = row["CheckedState"].ToString();
                    if (row["CheckDate"].ToString() != "")
                    {
                        item.CheckDate = DateTime.Parse(row["CheckDate"].ToString());
                    }
                    if (row["CheckAdminID"].ToString() != "")
                    {
                        item.CheckAdminID = new int?(int.Parse(row["CheckAdminID"].ToString()));
                    }
                    item.CheckRemark = row["CheckRemark"].ToString();
                    item.OrganName = row["OrganName"].ToString();
                    item.RoleName = row["RoleName"].ToString();
                    item.LevelName = row["LevelName"].ToString();
                    item.TypeName = row["TypeName"].ToString();
                    item.BuildName = row["BuildName"].ToString();
                    item.OrganName2 = row["OrganName2"].ToString();
                    item.PayWay = row["PayWay"].ToString();
                    if (row["IsOffice"].ToString() != "")
                    {
                        item.IsOffice = int.Parse(row["IsOffice"].ToString());
                    }
                    if (row["LevelNo"].ToString() != "")
                    {
                        item.LevelNo = new int?(int.Parse(row["LevelNo"].ToString()));
                    }
                    if (row["ParentID"].ToString() != "")
                    {
                        item.ParentID = new int?(int.Parse(row["ParentID"].ToString()));
                    }
                    item.ParentName = row["ParentName"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Update(OrganChangeLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganChangeLog set ");
            builder.Append(" OrganClassID = @OrganClassID , ");
            builder.Append(" OrganRoleID = @OrganRoleID , ");
            builder.Append(" OrganLevelID = @OrganLevelID , ");
            builder.Append(" OrganTypeID = @OrganTypeID , ");
            builder.Append(" OrganBuildID = @OrganBuildID , ");
            builder.Append(" OrganDescription = @OrganDescription , ");
            builder.Append(" OrganBranchCode = @OrganBranchCode , ");
            builder.Append(" PermitNo = @PermitNo , ");
            builder.Append(" Functions = @Functions , ");
            builder.Append(" BankName = @BankName , ");
            builder.Append(" OrganID = @OrganID , ");
            builder.Append(" BankID = @BankID , ");
            builder.Append(" LegalPerson = @LegalPerson , ");
            builder.Append(" PermitOrganCount = @PermitOrganCount , ");
            builder.Append(" RealityOrganCount = @RealityOrganCount , ");
            builder.Append(" Address = @Address , ");
            builder.Append(" Postcode = @Postcode , ");
            builder.Append(" Contact = @Contact , ");
            builder.Append(" Tel = @Tel , ");
            builder.Append(" Remark = @Remark , ");
            builder.Append(" AddDate = @AddDate , ");
            builder.Append(" OrganName = @OrganName , ");
            builder.Append(" AddUser = @AddUser , ");
            builder.Append(" UpdateDate = @UpdateDate , ");
            builder.Append(" UpdateUser = @UpdateUser , ");
            builder.Append(" LogType = @LogType , ");
            builder.Append(" CheckedState = @CheckedState , ");
            builder.Append(" CheckDate = @CheckDate , ");
            builder.Append(" CheckAdminID = @CheckAdminID , ");
            builder.Append(" CheckRemark = @CheckRemark , ");
            builder.Append(" OrganName2 = @OrganName2 , ");
            builder.Append(" PayWay = @PayWay , ");
            builder.Append(" IsOffice = @IsOffice , ");
            builder.Append(" LevelNo = @LevelNo , ");
            builder.Append(" ParentID = @ParentID , ");
            builder.Append(" ParentName = @ParentName , ");
            builder.Append(" OrganCode = @OrganCode  ");
            builder.Append(" where LogID=@LogID ");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@LogID", model.LogID), new SqlParameter("@OrganClassID", model.OrganClassID), new SqlParameter("@OrganRoleID", model.OrganRoleID), new SqlParameter("@OrganLevelID", model.OrganLevelID), new SqlParameter("@OrganTypeID", model.OrganTypeID), new SqlParameter("@OrganBuildID", model.OrganBuildID), new SqlParameter("@OrganDescription", model.OrganDescription), new SqlParameter("@OrganBranchCode", model.OrganBranchCode), new SqlParameter("@PermitNo", model.PermitNo), new SqlParameter("@Functions", model.Functions), new SqlParameter("@BankName", model.BankName), new SqlParameter("@OrganID", model.OrganID), new SqlParameter("@BankID", model.BankID), new SqlParameter("@LegalPerson", model.LegalPerson), new SqlParameter("@PermitOrganCount", model.PermitOrganCount), new SqlParameter("@RealityOrganCount", model.RealityOrganCount), 
                new SqlParameter("@Address", model.Address), new SqlParameter("@Postcode", model.Postcode), new SqlParameter("@Contact", model.Contact), new SqlParameter("@Tel", model.Tel), new SqlParameter("@Remark", model.Remark), new SqlParameter("@AddDate", model.AddDate), new SqlParameter("@OrganName", model.OrganName), new SqlParameter("@AddUser", model.AddUser), new SqlParameter("@UpdateDate", model.UpdateDate), new SqlParameter("@UpdateUser", model.UpdateUser), new SqlParameter("@LogType", model.LogType), new SqlParameter("@CheckedState", model.CheckedState), new SqlParameter("@CheckDate", model.CheckDate), new SqlParameter("@CheckAdminID", model.CheckAdminID), new SqlParameter("@CheckRemark", model.CheckRemark), new SqlParameter("@OrganName2", model.OrganName2), 
                new SqlParameter("@PayWay", model.PayWay), new SqlParameter("@IsOffice", model.IsOffice), new SqlParameter("@LevelNo", model.LevelNo), new SqlParameter("@ParentID", model.ParentID), new SqlParameter("@ParentName", model.ParentName), new SqlParameter("@OrganCode", model.OrganCode)
             };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public void UpdateOrganID(int logID, int organID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganChangeLog set ");
            builder.Append(" OrganID = @OrganID   ");
            builder.Append(" where LogID=@LogID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", logID), new SqlParameter("@OrganID", organID) };
            int num = DBHelper.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

